import request from "../utils/request";

// 定义 API 响应的接口
interface ApiResponse<T = any> {
  code: number;
  data: T;
  message: string;
}

// 定义请求参数的接口
interface RequestParams {
  [key: string]: any;
}

// 通用的请求处理函数，用于处理请求和响应
async function handleRequest<T = any>(url: string, method: 'GET' | 'POST', paramsOrData?: RequestParams): Promise<ApiResponse<T>> {
  try {
    // 根据请求方法发送请求
    const res = method === 'GET'
      ? await request.get(url, paramsOrData)
      : await request.post(url, paramsOrData);

    // 根据响应码处理成功或失败的情况
    if (res.code === 0) {
      // 成功的提示
      console.log('Success:', res.message);
    } else {
      // 失败的提示
      // console.error('Error:', res.message);
      console.log('Error:', res.message);
    }
    return res;
  } catch (error) {
    // 捕获并打印请求错误
    console.error('Request error:', error);
    throw error;
  }
}

// 以下是具体的 API 请求函数，它们都使用 handleRequest 函数来发送请求

export const fetchLogin = async (data: RequestParams): Promise<ApiResponse> => handleRequest('/users/checkLogin', 'POST', data);               // 1、登录接口
export const fetchLoginTwo = async (): Promise<ApiResponse> => handleRequest('/users/info', 'GET');                                            // 2、获取账号列表
export const fetchLoginThree = async (params: RequestParams): Promise<ApiResponse> => handleRequest('/order/list', 'GET', params);             // 3、获取订单管理列表
export const fetchLoginFour = async (data: RequestParams): Promise<ApiResponse> => handleRequest('/order/edit', 'POST', data);                 // 4、编辑修改订单
export const fetchLoginFive = async (params: RequestParams): Promise<ApiResponse> => handleRequest('/goods/list', 'GET', params);              // 5、获取商品列表数据
export const fetchLoginSix = async (params: RequestParams): Promise<ApiResponse> => handleRequest('/goods/del', 'GET', params);                // 6、删除商品
export const fetchLoginSeven = async (data: RequestParams): Promise<ApiResponse> => handleRequest('/goods/edit', 'POST', data);                // 7、修改商品
export const fetchLoginEight = async (data: RequestParams): Promise<ApiResponse> => handleRequest('/goods/goods_img_upload', 'POST', data);    // 8、上传商品图片
export const fetchLoginNine = async (params: RequestParams): Promise<ApiResponse> => handleRequest('/goods/catelist', 'GET', params);          // 9、获取商品分类
export const fetchLoginTen = async (data: RequestParams): Promise<ApiResponse> => handleRequest('/goods/add', 'POST', data);                   // 10、新增商品
export const fetchLoginEve = async (data: RequestParams): Promise<ApiResponse> => handleRequest('/goods/editcate', 'POST', data);              // 11、修改商品分类
export const fetchLoginTwi = async (params: RequestParams): Promise<ApiResponse> => handleRequest('/goods/delcate', 'GET', params);            // 12、删除商品分类
export const fetchLoginthirteen = async (data: RequestParams): Promise<ApiResponse> => handleRequest('/goods/addcate', 'POST', data);          // 13、新增商品分类
export const fetchLoginFourteen = async (): Promise<ApiResponse> => handleRequest('/shop/info', 'GET');                                        // 14、获取店铺详情
export const fetchLoginFifteen = async (data: RequestParams): Promise<ApiResponse> => handleRequest('/shop/edit', 'POST', data);               // 15、修改店铺内容
export const fetchLoginSixteen = async (params: RequestParams): Promise<ApiResponse> => handleRequest('/users/list', 'GET', params);           // 16、获取账号列表
export const fetchLoginSeventeen = async (params: RequestParams): Promise<ApiResponse> => handleRequest('/users/batchdel', 'GET', params);     // 17、账号批量删除
export const fetchLoginEighten = async (data: RequestParams): Promise<ApiResponse> => handleRequest('/users/edit', 'POST', data);              // 18、修改账号
export const fetchLoginNineteen = async (params: RequestParams): Promise<ApiResponse> => handleRequest('/users/del', 'GET', params);           // 19、单个删除账号
export const fetchLoginTwenty = async (data: RequestParams): Promise<ApiResponse> => handleRequest('/users/add', 'POST', data);                // 20、添加账号
export const fetchLoginTwentyOne = async (params: RequestParams): Promise<ApiResponse> => handleRequest('/users/checkoldpwd', 'GET', params);  // 21、检验旧密码是否正确
export const fetchLoginTwentyTwo = async (data: RequestParams): Promise<ApiResponse> => handleRequest('/users/editpwd', 'POST', data);         // 22、修改密码
export const fetchLoginTwentyThree = async (params: RequestParams): Promise<ApiResponse> => handleRequest('/users/avataredit', 'GET', params); // 23、修改用户头像
export const fetchLoginTwentyFour = async (params: RequestParams): Promise<ApiResponse> => handleRequest('/goods/goodstotal', 'GET', params);  // 24、商品统计
export const fetchLoginTwentyFive = async (params: RequestParams): Promise<ApiResponse> => handleRequest('/order/ordertotal', 'GET', params);  // 25、订单统计
export const fetchLoginTwentySix = async (params: RequestParams): Promise<ApiResponse> => handleRequest('/order/search', 'GET', params);       // 26、订单查询
